import 'package:eh_mobile/common/routers/index.dart';
import 'package:eh_mobile/common/style/bny_color.dart';
import 'package:eh_mobile/pages/frame/login/bind/bind_index.dart';
import 'package:eh_mobile/pages/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../../../common/utils/nav_bar_title.dart';

class BindDeviceLoginPage extends StatefulWidget {
  final String codeStr;
  final String nickNameStr;
  final Function refreshBlock;

  const BindDeviceLoginPage(
      {Key? key,
      required this.codeStr,
      required this.nickNameStr,
      required this.refreshBlock})
      : super(key: key);

  @override
  State<BindDeviceLoginPage> createState() => _BindDeviceLoginPageState();
}

class _BindDeviceLoginPageState extends State<BindDeviceLoginPage> {
  TextStyle fontStyle = TextStyle(
      fontSize: 14, fontWeight: FontWeight.w400, color: HexColor('#969696'));
  BindDeviceLoginController bindController =
      Get.put(BindDeviceLoginController());

  @override
  Widget build(BuildContext context) {
    return LoginUtils.loginBgView(
      child: NavBarTitle(
        name: '账号绑定登录',
        backBlock: () {
          backToLoginView();
        },
        childWidget: SizedBox(
          width: 1.sw,
          height: 1.sh,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                margin: EdgeInsets.only(top: 107.h),
                width: 182.h,
                child: GetBuilder(
                    id: bindController.circleProgressViewId,
                    builder: (BindDeviceLoginController controller) {
                      return CircleProgressPercentPage(
                        foregroundColor: HexColor('#0096D0'),
                        backgroundColor: HexColor('#323232'),
                        value: controller.isConnectSuccess
                            ? controller.progress
                            : 0,
                        strokeWidth: 2.0.w,
                        child: controller.isConnectSuccess
                            ? SvgPicture.asset(
                                'assets/svgs/login/login_bind_tv.svg',
                                width: 46.w,
                                height: 38.w,
                                fit: BoxFit.scaleDown,
                              )
                            : Center(
                                child: Text(
                                  '绑定失败',
                                  style: TextStyle(
                                      fontSize: 22,
                                      fontWeight: FontWeight.w400,
                                      color: HexColor('#FA6D5A')),
                                ),
                              ),
                      );
                    }),
              ),
              Container(
                margin: EdgeInsets.only(top: 116.h),
                child: GetBuilder(
                    id: bindController.richTextViewId,
                    builder: (BindDeviceLoginController controller) {
                      return RichText(
                        textAlign: TextAlign.center,
                        text: TextSpan(
                          text: controller.isConnectSuccess
                              ? '正在尝试使用移动端与用户 '
                              : '发生错误，请尝试',
                          style: fontStyle,
                          children: controller.isConnectSuccess
                              ? [
                                  TextSpan(
                                    text: '${widget.nickNameStr}\n',
                                    style: TextStyle(
                                        fontSize: 15,
                                        color: HexColor('#DEDEDE'),
                                        fontWeight: FontWeight.w600),
                                  ),
                                  TextSpan(
                                    text: '进行绑定登录，请确认是否为本人操作！',
                                    style: fontStyle,
                                  )
                                ]
                              : [],
                        ),
                      );
                    }),
              ),
              SizedBox(
                height: 26.h,
              ),
              GetBuilder(
                  id: bindController.bindBtnId,
                  builder: (BindDeviceLoginController controller) {
                    return LoginUtils.cornerTextView(
                        controller.isConnectSuccess ? '确认绑定 ' : '返回', () {
                      if (controller.isConnectSuccess) {
                        bindController.mobileBindUrl(widget.codeStr);
                      } else {
                        backToLoginView();
                      }
                    });
                  }),
              SizedBox(
                height: 25.w,
              ),
              GetBuilder(
                  id: bindController.cancelBtnId,
                  builder: (BindDeviceLoginController controller) {
                    return Visibility(
                      visible: controller.isConnectSuccess,
                      child: TextButton(
                          onPressed: () {
                            backToLoginView();
                          },
                          child: Text(
                            '取消',
                            style: TextStyle(
                                fontSize: 22,
                                color: HexColor('#0096D0'),
                                fontWeight: FontWeight.w600),
                          )),
                    );
                  })
            ],
          ),
        ),
      ),
    );
  }

  void backToLoginView() {
    Navigator.popUntil(context, ModalRoute.withName(RouteNames.LOGIN));
  }

  @override
  void initState() {
    super.initState();
    bindController.refreshBlock = widget.refreshBlock;
  }
}
